using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.PackageManager;
using UnityEngine;

public class UI : MonoBehaviour
{
    [SerializeField] private List<GameObject> _panelUIList = new();
    public PlayerInput UIInput { get; private set; }
    public UI_Dialogue DialogueUI { get; private set; }
    public UI_InGame InGameUI { get; set; }
    public UI_BackPack InventoryUI { get; private set; }
    public UI_WeaponSelect WeaponSelectUI { get; private set; }
    public UI_ScrollSelect ScrollSelectUI { get; private set; }
    private void Awake()
    {
        DialogueUI = GetComponentInChildren<UI_Dialogue>(true);
        InGameUI = GetComponentInChildren<UI_InGame>(true);
        InventoryUI = GetComponentInChildren<UI_BackPack>(true);
        WeaponSelectUI = GetComponentInChildren<UI_WeaponSelect>(true);
        ScrollSelectUI = GetComponentInChildren<UI_ScrollSelect>(true);
    }
    
    private void OnEnable()
    {
        EventCenter.AddEventListener<DialogueDataBase, Action>(EventName.UI_OpenDialoguePanel, OpenDialogueUI);
        EventCenter.AddEventListener<List<ItemDataSO>, Dictionary<int, ItemDataSO>, PlayerStatGroup>(EventName.UI_OpenBackPackPanel, OpenBackPackUI);
        EventCenter.AddEventListener<List<WeaponScrollDataSO>>(EventName.UI_OpenWeaponSelectPanel, OpenWeaponSelectUI);
        EventCenter.AddEventListener<SlotScorllDataSO[]>(EventName.UI_OpenSlotSelectPanel, OpenScrollSelectUI);
    }
    private void OnDisable()
    {
        EventCenter.RemoveEventListener<DialogueDataBase, Action>(EventName.UI_OpenDialoguePanel, OpenDialogueUI);
        EventCenter.RemoveEventListener<List<ItemDataSO>, Dictionary<int, ItemDataSO>, PlayerStatGroup>(EventName.UI_OpenBackPackPanel, OpenBackPackUI);
        EventCenter.RemoveEventListener<List<WeaponScrollDataSO>>(EventName.UI_OpenWeaponSelectPanel, OpenWeaponSelectUI);
        EventCenter.RemoveEventListener<SlotScorllDataSO[]>(EventName.UI_OpenSlotSelectPanel, OpenScrollSelectUI);
    }

    public void SetUI(PlayerInput playerInput)
    {
        UIInput = playerInput;
        UIInput.UI.DialogueInteraction.performed += context =>
        {
            if (DialogueUI.gameObject.activeSelf)
                DialogueUI.DialogueInteraction();
        };
        UIInput.UI.CloseAllPanel.performed += context => CloseAllPanel();
    }
    /// <summary>
    /// 打开对话UI
    /// </summary>
    /// <param name="curentDialogueDataSO"></param>
    public void OpenDialogueUI(DialogueDataBase curentDialogueDataSO, Action callBackInEndOfDialogue)
    {
        InitUIPanel();

        DialogueUI.gameObject.SetActive(true);

        DialogueUI.SetupDialogueUI(curentDialogueDataSO, callBackInEndOfDialogue);
    }

    /// <summary>
    /// 打开背包UI
    /// </summary>
    /// <param name="itemDataSOList"></param>
    /// <param name="equipmentDict"></param>
    public void OpenBackPackUI(List<ItemDataSO> itemDataSOList, Dictionary<int, ItemDataSO> equipmentDict, PlayerStatGroup playerStatGroup)
    {
        InitUIPanel();

        InventoryUI.gameObject.SetActive(true);

        InventoryUI.SetupBackPackUI(itemDataSOList, equipmentDict, playerStatGroup);
    }
    
    /// <summary>
    /// 打开武器选择UI
    /// </summary>
    /// <param name="itemDataSOList"></param>
    /// <param name="equipmentDict"></param>
    public void OpenWeaponSelectUI(List<WeaponScrollDataSO> weaponItemDataList)
    {
        InitUIPanel();

        WeaponSelectUI.gameObject.SetActive(true);

        WeaponSelectUI.SetupWeaponSelectUI(weaponItemDataList);
    }

    /// <summary>
    /// 打开卷轴UI
    /// </summary>
    /// <param name="itemDataSOList"></param>
    /// <param name="equipmentDict"></param>
    public void OpenScrollSelectUI(SlotScorllDataSO[] scorllDataSOList)
    {
        InitUIPanel();

        ScrollSelectUI.gameObject.SetActive(true);

        ScrollSelectUI.SetupScrollSelect(scorllDataSOList);
    }
    private void InitUIPanel()
    {
        foreach (var panel in _panelUIList)
            panel.SetActive(false);

        EnableUIInput(true);
    }
    private void CloseAllPanel()
    {
        foreach (var panel in _panelUIList)
            panel.SetActive(false);
            
        EnableUIInput(false);
    }
    public void EnableUIInput(bool isOpenUI)
    {
        if (isOpenUI)
        {
            UIInput.UI.Enable();
            UIInput.Player.Disable();
            
        }
        else
        {
            UIInput.UI.Disable();
            UIInput.Player.Enable();
        }
    }
}
